package com.googlecode.npgame.levels;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.googlecode.npgame.simulation.ElementType;

/**
 * @author Josh (joshbruning)
 */
public class Level {

  private int bufferSize;
  private int frameVolume;
  private int elementDrain;
  private boolean wildCard;
  private List<ElementType> colors;
  private List<ElementType> data;
  private int expectedTime;

  public Level() {
    this.bufferSize = 0;
    this.frameVolume = 0;
    this.elementDrain = 0;
    this.wildCard = false;
    this.colors = new ArrayList<ElementType>();
    this.data = new ArrayList<ElementType>();
    this.expectedTime = 0;
  }

  public Level(Document document) {
    Element root = document.getDocumentElement();
    this.bufferSize = Integer.parseInt(root.getAttribute("bufferSize"));
    this.frameVolume = Integer.parseInt(root.getAttribute("frameVolume"));
    this.elementDrain = Integer.parseInt(root.getAttribute("elementDrain"));
    this.wildCard = Boolean.parseBoolean(root.getAttribute("wildCard"));
    this.expectedTime = Integer.parseInt(root.getAttribute("expectedTime"));
    Node colorsNode = root.getElementsByTagName("colors").item(0);
    Node dataNode = root.getElementsByTagName("data").item(0);
    NodeList colorsList = colorsNode.getChildNodes();
    NodeList dataList = dataNode.getChildNodes();
    this.colors = new ArrayList<ElementType>();
    this.data = new ArrayList<ElementType>();
    for (int i = 0; i < colorsList.getLength(); i++) {
      Node child = colorsList.item(i);
      if (child instanceof Element) {
        Element childElement = (Element) child;
        this.colors.add(ElementType.valueOf(childElement.getAttribute("name")));
      }
    }
    for (int i = 0; i < dataList.getLength(); i++) {
      Node child = dataList.item(i);
      if (child instanceof Element) {
        Element childElement = (Element) child;
        this.colors
            .add(ElementType.valueOf(childElement.getAttribute("color")));
      }
    }
  }

  public int getBufferSize() {
    return bufferSize;
  }

  public int getFrameVolume() {
    return frameVolume;
  }

  public int getElementDrain() {
    return elementDrain;
  }

  public boolean isWildCard() {
    return wildCard;
  }

  public List<ElementType> getColors() {
    return colors;
  }

  public List<ElementType> getData() {
    return data;
  }

  public int getExpectedTime() {
    return expectedTime;
  }
}
